<?php

/** Copyright 2014 TinPhp Systems, Inc.
 * @author 2014-10-22
 */
if (!defined('IN_TINPHP'))
{
    exit('Access Denied');
}

/**
 * 理财产品业务逻辑类
 * 
 * 每个 Logic 对应一个业务逻辑处理单元、必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class DealLogic extends TinBaseLogic
{

    private $DealDao;

    // 每个 Logic 需要有一个构造函数
    public function DealLogic()
    {
        parent::__construct();
        $this->logger->className = "DealLogic";
        $this->dealDao = Tin::getDAO("AssignTable", "DEAL");
        $this->dealLoadDao = Tin::getDAO("AssignTable", "DEAL_LOAD");
        $this->dealUserView = Tin::getDAO("AssignView","V_DEAL_USER");
        $this->dealLoadDealView = Tin::getDAO("AssignView","V_DEAL_LOAD_DEAL");
    }

    public function __destruct()
    {
        $this->dealDao = null;
        $this->dealLoadDao = null;
    }

    /************* bof 理财 借款 ********************/
    
    /** 获取一条项目 */
    public function getDealItem($cols, $cond)
    {
        return $this->dealDao->selectRowByCond($cols, $cond);
    }

    /** 获取多条项目及数量 */
    public function getDealList($cols, $cond, $order_by = null, $page = null, $size = null)
    {
        $res = array();
        $res["data"] = $this->dealDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
        if ($page)
        {
            $res["count"] = $this->dealDao->getCountByCond($cond);
        }
        return $res;
    }
    
    /** 获取多条项目及数量 */
    public function getDealUserViewList($cols, $cond, $order_by = null, $page = null, $size = null)
    {
        $res = array();
        $res["data"] = $this->dealUserView->selectByCond($cols, $cond . " " . $order_by, $page, $size);
        if ($page)
        {
            $res["count"] = $this->dealUserView->getCountByCond($cond);
        }
        return $res;
    }
           
    /** 添加项目 */
    public function addDealItem($data)
    {
        $map = Tin :: createDataMap();
        $map->addEntry('TYPE', $data['type'], DB::INT);
        
        $code = date("YmdHis");
        $map->addEntry('CODE', 'LC' . $code, DB::VARCHAR);
        
        $map->addEntry('FINANCE', $data['finance'], DB::VARCHAR);
        $map->addEntry('USER_ID', $data['user_id'], DB::INT);
        $map->addEntry('DEAL_PERSON', $data['deal_person'], DB::INT);
        $map->addEntry('TITLE', $data['title'], DB::VARCHAR);        
        $map->addEntry('PRICE', $data['price'], DB::DOUBLE);
        $map->addEntry('MAX_LOAD', $data['max_load'] ? $data['max_load'] : 0.00, DB::DOUBLE);
        
        if((int)$data['type'] == 2){
            $data['price_allow'] = $data['price'];
            $data['state_allow'] = 1; // 1 同意 2 不同意
        }
        $map->addEntry('PRICE_ALLOW', $data['price_allow'], DB::DOUBLE);
        $map->addEntry('STATE_ALLOW', $data['state_allow'], DB::INT);
        
        $map->addEntry('RATE', $data['rate'], DB::INT);
        $map->addEntry('REPAY_TIME', $data['repay_time'], DB::INT);
        $map->addEntry('TYPE_BORROW', $data['type_borrow'], DB::INT);
        $map->addEntry('TYPE_REPAY', $data['type_repay'], DB::INT);
        $map->addEntry('TYPE_LOAN', $data['type_loan'], DB::INT);
        $map->addEntry('REPAY_START_TIME', $data['repay_start_time'], DB::VARCHAR);
        $map->addEntry('BAD_TIME', $data['bad_time'], DB::VARCHAR);
        $map->addEntry('CONTENT', $data['content'], DB::TEXT);        
        $map->addEntry('DEAL_STAT', $data['deal_stat'], DB::DATETIME);
        $map->addEntry('STATE', $data['state'], DB::DATETIME);
        $map->addEntry('SORT_NUM', $data['sort_num'] ? $data['sort_num'] : 0, DB::INT);
        $map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);
        $map->addEntry('UPDATE_TIME', 'now()', DB::DATETIME);        
        try
        {
            $res = $this->dealDao->insert($map, false, true);
            return $res;
        } catch (Exception $e)
        {
            $this->logger->error("addDealItem:" . $e->getMessage());
            echo $e->getMessage();
            return null;
        }
    }
    
    /** 更新项目 */
    public function updateDealItem($data, $cond)
    {
        try
        {
            $this->beginTrans(); //开启事务

            $map = Tin :: createDataMap();
            $map->addEntry('FINANCE', $data['finance'], DB::VARCHAR);
            $map->addEntry('TITLE', $data['title'], DB::VARCHAR);            
            $map->addEntry('PRICE', $data['price'], DB::INT);
            $map->addEntry('MAX_LOAD', $data['max_load'] ? $data['max_load'] : 0.00, DB::DOUBLE);
            
            if((int)$data['type'] == 2){
                $data['price_allow'] = $data['price'];            
            }
            $map->addEntry('PRICE_ALLOW', $data['price_allow'], DB::INT);            
            
            $map->addEntry('RATE', $data['rate'], DB::INT);
            $map->addEntry('REPAY_TIME', $data['repay_time'], DB::INT);
            $map->addEntry('TYPE_BORROW', $data['type_borrow'], DB::INT);
            $map->addEntry('TYPE_REPAY', $data['type_repay'], DB::INT);
            $map->addEntry('TYPE_LOAN', $data['type_loan'], DB::INT);
            $map->addEntry('REPAY_START_TIME', $data['repay_start_time'], DB::VARCHAR);
            $map->addEntry('BAD_TIME', $data['bad_time'], DB::VARCHAR);
            $map->addEntry('CONTENT', $data['content'], DB::TEXT);
            $map->addEntry('DEAL_STAT', $data['deal_stat'], DB::DATETIME);
            $map->addEntry('STATE', $data['state'], DB::INT);
            $map->addEntry('SORT_NUM', $data['sort_num'] ? $data['sort_num'] : 0, DB::INT);
            $map->addEntry('UPDATE_TIME', 'now()', DB::DATETIME);
            $res = $this->dealDao->updateByCond($map, $cond, false);

            $this->endTrans(); //结束并提交事务
            return $res;
        } catch (Exception $e)
        {
            $this->logger->error("updateDealItem:" . $e->getMessage());
            $this->giveUpTrans();   //事务失败回滚
            return false;
        }
    }
    
    /** 删除项目 */
    public function delDealItem($cond, $params = false)
    {
        return $this->dealDao->deleteByCond($cond, $params);
    }
    
    /************* eof 理财 借款 ********************/
    
    /************* bof 投资记录 ********************/
    
    /** 获取一条投资记录 */
    public function getDealLoadItem($cols, $cond)
    {
        return $this->dealLoadDao->selectRowByCond($cols, $cond);
    }

    /** 获取多条投资记录及数量 */
    public function getDealLoadList($cols, $cond, $order_by = null, $page = null, $size = null)
    {
        $res = array();
        $res["data"] = $this->dealLoadDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
        if ($page)
        {
            $res["count"] = $this->dealLoadDao->getCountByCond($cond);
        }
        return $res;
    }
    
    /** 获取多条投资记录及数量 */
    public function getDealLoadDealViewList($cols, $cond, $order_by = null, $page = null, $size = null)
    {
        $res = array();
        $res["data"] = $this->dealLoadDealView->selectByCond($cols, $cond . " " . $order_by, $page, $size);
        if ($page)
        {
            $res["count"] = $this->dealLoadDealView->getCountByCond($cond);
        }
        return $res;
    }
    
    /** 添加投资记录 */
    public function addDealLoadItem($data)
    {
        $map = Tin :: createDataMap();
        $map->addEntry('DEAL_ID', $data["deal_id"], DB::INT);
        $map->addEntry('USER_ID', $data["user_id"], DB::INT);
        //$map->addEntry('INFO_ID', $data["info_id"], DB::INT);
        $map->addEntry('MONEY', $data["money"], DB::DECIMAL);
        $map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);  
        try
        {
            $res = $this->dealLoadDao->insert($map, false, true);
            return $res;
        } catch (Exception $e)
        {
            $this->logger->error("addDealLoadItem:" . $e->getMessage());
            echo $e->getMessage();
            return null;
        }
    }

    /** 删除投资记录 */
    public function delDealLoadItem($cond, $params = false)
    {
        return $this->dealLoadDao->deleteByCond($cond, $params);
    }

    /************* eof 投资记录 ********************/

    /* 更新指定字段 */
    public function updateDealVal($dataArr, $cond, $params=false){
        if(!is_array($dataArr))return false;
        $map = Tin :: createDataMap();
        foreach ($dataArr as $k=>$v){
            $map->addEntry(strtoupper($k), $v, DB::VARCHAR);
        }
        try{
            return $this->dealDao->updateByCond($map, $cond, false, $params);
        }catch(Exception $e){
            $this->logger->error("updateDealVal:".$e->getMessage());
            return false;
        }
    }

}

//End Class
?>
